﻿using System;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf;

namespace Sharp.Interprocess.MemoryManager
{
    [ProtoContract]
    public class SegmentHandle : IEquatable<SegmentHandle>
    {
        [ProtoMember(1,DataFormat = DataFormat.FixedSize)]
        public int HandleId { get; private set; }

        /// <summary>
        /// collection of allocated segments for data storage.
        ///  key -> offset of data chunk stored in the segment
        /// </summary>
        [ProtoMember(2)]
        public ICollection<Segment> Segments { get; private set; }

        internal SegmentHandle()
        {
            Segments = new List<Segment>();
        }

        internal SegmentHandle(int handleId)
        {
            HandleId = handleId;
            Segments = new List<Segment>();
        }

        #region Object / Misc overloads

        public override int GetHashCode()
        {
            int hashCode = 17;
            hashCode = 23 * hashCode + HandleId.GetHashCode();
            hashCode = 23 * hashCode + Segments.GetHashCode();
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            var otherSegmentHandle = obj as SegmentHandle;
            if (otherSegmentHandle == null) return false;

            return Equals(otherSegmentHandle);
        }

        public bool Equals(SegmentHandle other)
        {
            return other.HandleId == HandleId;
        }

        #endregion
    }
}
